import tkinter as tk
from tkinter import ttk
from ..utils.logger import AppLogger
from .tool_tree import ToolTree
from .log_panel import LogPanel
from ..services.i18n_manager import I18nManager

class MainWindow:
    def __init__(self, root, config):
        self.root = root
        self.config = config
        self.logger = AppLogger()

        self._setup_ui()
        self.i18n = I18nManager()
        self._setup_language_menu()

    def _setup_ui(self):
        # 主布局
        self.main_frame = ttk.PanedWindow(self.root, orient=tk.HORIZONTAL)
        self.main_frame.pack(fill=tk.BOTH, expand=True)

        # 左侧工具树
        self.tool_tree = ToolTree(self.main_frame, self.config, self.logger)
        self.main_frame.add(self.tool_tree.frame)

        # 右侧日志面板
        self.log_panel = LogPanel(self.main_frame)
        self.main_frame.add(self.log_panel.frame)

        # 状态栏
        self.status_bar = ttk.Label(self.root, text="就绪", relief=tk.SUNKEN)
        self.status_bar.pack(side=tk.BOTTOM, fill=tk.X)

    def _setup_language_menu(self):
        menubar = tk.Menu(self.root)
        lang_menu = tk.Menu(menubar, tearoff=0)

        for code, name in self.i18n.get_available_languages():
            lang_menu.add_radiobutton(
                label=name,
                command=lambda c=code: self._change_language(c)
            )

        menubar.add_cascade(label=_("语言"), menu=lang_menu)
        self.root.config(menu=menubar)

    def _change_language(self, lang_code):
        self.i18n.set_language(lang_code)
        # 刷新所有界面文字
        self.tool_tree.refresh_text()
        self.log_panel.refresh_text()
        self.status_bar.config(text=_("就绪"))